package com.sos.core.game
{
	
	import com.sos.core.DispatcherDataObject;
	import com.sos.core.Position;
	
	import flash.events.Event;

	public class GameBall extends DispatcherDataObject
	{
	
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON data property names
		protected static const		JSON_PROPERTY_BALL:String					= "ball";
		
		// Events
		public static const         EVENT_GAME_BALL_CHANGED:String				= "game_ball_changed";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _position:Position;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameBall()
		{
			super();
			// Clears data
			clear();
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		[Bindable(event="game_ball_changed")]
		public function get position():Position
		{
			return _position;
		}
		
		public function set position(position:Position):void
		{
			_position = position;
		}
		
		[Bindable(event="game_ball_changed")]
		public function get oppositePosition():Position
		{
			return Position.getOpposite(_position);
		}
				
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		// DataObject implementation
		
		override protected function validate(data:Object):void
		{
			if (!data.hasOwnProperty(GameBall.JSON_PROPERTY_BALL)) throwLoadMissingPropError(JSON_PROPERTY_BALL);
		}
		
		override protected function fill(data:Object):void
		{
			_position = Position.getBySign(data[GameBall.JSON_PROPERTY_BALL]);
		}
		
		// Event dispatchers
		
		protected function dispatchGameBallChanged():void
		{
			dispatchEvent(new Event(EVENT_GAME_BALL_CHANGED));
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function equals(gameBall:GameBall):Boolean
		{
			return	_position == gameBall.position;
		}
		
		override public function clear():void
		{
			// Clear data
			_position = Position.MIDFIELD;
		}
	
	}
	
}